#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int r;
    struct Node* next;
} Node;

int process(int P, int E, int I, int D)
{
    int i = 0, temp = 0;
    if(P>=23)
        P=P%23;
    if(E>=28)
        E=E%28;
    if(I>=33)
        I=I%33;
    for(i=0; i<=21252; i++)
    {
        temp=i+D;
        if(temp%23==P&&temp%28==E&&temp%33==I&&temp!=D)
            break;
    }
    return i;

}
void getinputs(Node** head)
{
    int p = 0, e = 0, i = 0, d = 0;
    Node *q = NULL, *pre = NULL;

    scanf("%d %d %d %d", &p, &e, &i, &d);
    while(!(p == e && e == i && i == d && d == -1))
    {
        q = (Node*)malloc(sizeof(Node));
        q->next = NULL;

        q->r = process(p, e, i, d);

        if(*head == NULL)
        {
            *head = q;
        }
        else
        {
            pre->next = q;
        }
        pre = q;

        scanf("%d %d %d %d", &p, &e, &i, &d);
    }
};

void show(Node* head)
{
    Node* p = NULL;
    int i = 1;

    for(p = head; p != NULL; p = p->next, i++)
    {
        printf("Case %d: the next triple peak occurs in %d days.\r\n", i, p->r);
    }
};

int main()
{
    Node* head = NULL;

    getinputs(&head);

    show(head);

    return 0;
}
